//
//  BDMerchantManageViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/12.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "BDMerchantManageViewController.h"
#import "UITableView+HSYBase.h"

#import "BDPreAuditCell.h"
#import "BDAuditFailCell.h"
#import "BDInfoNotSubmitCell.h"

#import "BDBusinessListModel.h"
#import "BDGetManageBusinessListRequest.h"
#import "UIViewController+BDAddBusinessHelper.h"

#import "BQActivityView.h"
#import "BDGetShopInfoRequest.h"


static NSString * const preAuditCellReuseIdentifier = @"preAuditCellReuseIdentifier";
static NSString * const auditFailCellReuseIdentifier = @"auditFailCellReuseIdentifier";
static NSString * const infoNotSubmitCellReuseIdentifier = @"infoNotSubmitCellReuseIdentifier";


@interface BDMerchantManageViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *preAuditBtn;
@property (weak, nonatomic) IBOutlet UIButton *auditFailBtn;
@property (weak, nonatomic) IBOutlet UIButton *infoNotSubmitBtn;

@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;

@property (weak, nonatomic) IBOutlet UITableView *tableView_1;
@property (weak, nonatomic) IBOutlet UITableView *tableView_2;
@property (weak, nonatomic) IBOutlet UITableView *tableView_3;

@property (nonatomic, strong) BDBusinessListModel *preAuditListModel;
@property (nonatomic, strong) BDBusinessListModel *auditFailListModel;
@property (nonatomic, strong) BDBusinessListModel *infoNotSubmitListModel;

@end

@implementation BDMerchantManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.titleButton setTitle:@"报单中的商户" forState:UIControlStateNormal];
    [self setupAllTableViews];
    
    [self.tableView_1.mj_header beginRefreshing];
    
    [self unSelectAllBtns];
    self.preAuditBtn.selected = YES;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.tableView_2.mj_header beginRefreshing];
    [self.tableView_3.mj_header beginRefreshing];

}

//初始化各个tableview
- (void)setupAllTableViews{
    //数据加载
    [self.tableView_1 addMJ_HeaderWithTarget:self selector:@selector(loadPreAuditData)];
    [self.tableView_1 addMJ_FooterWithTarget:self selector:@selector(loadPreAuditData)];
    
    [self.tableView_2 addMJ_HeaderWithTarget:self selector:@selector(loadAuditFailData)];
    [self.tableView_2 addMJ_FooterWithTarget:self selector:@selector(loadAuditFailData)];
    
    [self.tableView_3 addMJ_HeaderWithTarget:self selector:@selector(loadInfoNotSubmitData)];
    [self.tableView_3 addMJ_FooterWithTarget:self selector:@selector(loadInfoNotSubmitData)];
    
    //无数据提示图片
    [self.tableView_1 setupMessage:@"对不起，暂无结果" imageName:@"empty"];
    [self.tableView_2 setupMessage:@"对不起，暂无结果" imageName:@"empty"];
    [self.tableView_3 setupMessage:@"对不起，暂无结果" imageName:@"empty"];
    
    //无数据时不显示分割线
    self.tableView_1.tableFooterView = [[UIView alloc] init];
    self.tableView_2.tableFooterView = [[UIView alloc] init];
    self.tableView_3.tableFooterView = [[UIView alloc] init];

    //注册cell
    [self.tableView_1 registerNib:[UINib nibWithNibName:@"BDPreAuditCell" bundle:nil] forCellReuseIdentifier:preAuditCellReuseIdentifier];
    [self.tableView_2 registerNib:[UINib nibWithNibName:@"BDAuditFailCell" bundle:nil] forCellReuseIdentifier:auditFailCellReuseIdentifier];
    [self.tableView_3 registerNib:[UINib nibWithNibName:@"BDInfoNotSubmitCell" bundle:nil] forCellReuseIdentifier:infoNotSubmitCellReuseIdentifier];


}

//请求待审核数据列表
- (void)loadPreAuditData{
    [self loadDataWithPage:0];
}

//请求审核失败数据列表
- (void)loadAuditFailData{
    [self loadDataWithPage:1];
}

//请求资料未提交数据
- (void)loadInfoNotSubmitData{
    [self loadDataWithPage:2];
}


- (void)loadDataWithPage:(NSInteger)page{
    NSInteger auid = [BDUserAccount instatce].auid;
    NSInteger status = 0;
    NSInteger updateTimeSortType = 0;
    BDBusinessListModel *mListModel = nil;
    
    __weak UITableView *tableView = nil;
    __weak typeof(self)weakSelf = self;
    
    switch (page) {
        case 0:
        {
            status = 2;
            tableView = weakSelf.tableView_1;
            mListModel = weakSelf.preAuditListModel;
        }
            break;
        case 1:
        {
            status = 3;
            tableView = weakSelf.tableView_2;
            mListModel = weakSelf.auditFailListModel;
        }
            break;
        case 2:
        {
            status = 4;
            tableView = weakSelf.tableView_3;
            mListModel = weakSelf.infoNotSubmitListModel;
        }
            break;
            
        default:
            break;
    }
    
    BDGetManageBusinessListRequest *task = [[BDGetManageBusinessListRequest alloc] init];
    task.auid = auid;
    task.status = status;
    task.updateTimeSortType = updateTimeSortType;
    task.pageIndex = tableView.pageIndex;
    
    [BDNetworkManager POST:task completionHander:^(id responseObject, NSString *resultString) {
        if (!resultString && !responseObject) {//resultString = nil && !responseObject 取消请求
            [tableView loadServerDataComplete:kDataLoadCompleteStatusCancelled];
            return;
        }
        if (resultString.length > 0) {
            [tableView loadServerDataComplete:kDataLoadCompleteStatusFailed];
        }else{
            BDBusinessListModel *listModel = [BDBusinessListModel mj_objectWithKeyValues:responseObject];
            
            if (tableView.pageIndex != 1) {
                [mListModel.list addObjectsFromArray:listModel.list];
                listModel.list = mListModel.list;
            }
            
            if (page == 0) {
                weakSelf.preAuditListModel = listModel;
            }
            
            if (page == 1) {
                weakSelf.auditFailListModel = listModel;
            }
            
            if (page == 2) {
                weakSelf.infoNotSubmitListModel = listModel;
            }

            NSInteger totalPage = listModel.page.totalPage;
            
            if (totalPage<= tableView.pageIndex) {
                [tableView loadServerDataComplete:kDataLoadCompleteStatusNoMoreData];
            }else{
                [tableView loadServerDataComplete:kDataLoadCompleteStatusDefault];
            }
            
        }
    }];
}



- (void)unSelectAllBtns{
    self.preAuditBtn.selected = NO;
    self.auditFailBtn.selected = NO;
    self.infoNotSubmitBtn.selected = NO;
}


- (IBAction)preAuditBtnClick:(UIButton *)sender {
    [self unSelectAllBtns];
    sender.selected = YES;
    
    [self.contentScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}


- (IBAction)auditFailureBtnClick:(UIButton *)sender {
    [self unSelectAllBtns];
    sender.selected = YES;

    [self.contentScrollView setContentOffset:CGPointMake(ScreenWidth(), 0) animated:YES];
}

- (IBAction)infoNotSubmitBtnClick:(UIButton *)sender {
    [self unSelectAllBtns];
    sender.selected = YES;

    [self.contentScrollView setContentOffset:CGPointMake(2*ScreenWidth(), 0) animated:YES];
}

- (void)pushToShopInfoWithModel:(BDBusinessModel *)model{
    if (!model)return;
    
    model = [BDBusinessModel mj_objectWithKeyValues:[model mj_keyValues]];
    model.auStep =@"-1";
    __weak typeof(self) wSelf = self;
    [wSelf nextStepWihBusinessModel:model userShopModel:nil readOnly:YES];
    
}

//继续完善资料
- (void)continueFillInfoModel:(BDBusinessModel *)model{
    __weak typeof(self) wSelf = self;
    [wSelf nextStepWihBusinessModel:model userShopModel:nil readOnly:NO];

//    [self shopInfoWithModel:model complete:^(BDUserShopModel *userShopModel) {
//        [wSelf nextStepWihBusinessModel:model userShopModel:userShopModel];
//    }];
}

//审核失败，完善资料
- (void)failToFillInfoModel:(BDBusinessModel *)model{
    model.auStep = @"0"; //强制从第一步开始
    __weak typeof(self) wSelf = self;
    [wSelf nextStepWihBusinessModel:model userShopModel:nil readOnly:NO];

//    [self shopInfoWithModel:model complete:^(BDUserShopModel *userShopModel) {
//        [wSelf nextStepWihBusinessModel:model userShopModel:userShopModel];
//    }];

}

-(void)shopInfoWithModel:(BDBusinessModel *)model complete:(void(^)(BDUserShopModel *model)) complete{
    __weak typeof(self)weakSelf = self;
    [BQActivityView showActiviTy];
    
    NSString *uid = [NSString stringWithFormat:@"%ld",(long)model.uid];
    
    BDGetShopInfoRequest *task = [[BDGetShopInfoRequest alloc] init];
    task.uid = uid;
    
    [BDNetworkManager POST:task completionHander:^(id responseObject, NSString *resultString) {
        [BQActivityView hideActiviTy];
        
        if (responseObject) {
            BDUserShopModel *model = [BDUserShopModel mj_objectWithKeyValues:responseObject];
            if (complete) {
                complete(model);
            }
        }else{
            if (resultString) {
                [weakSelf showMessage:resultString];
            }
        }
    }];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.tableView_1) {
        return [self _tableView_1:tableView numberOfRowsInSection:section];
    }
    
    if (tableView == self.tableView_2) {
        return [self _tableView_2:tableView numberOfRowsInSection:section];
    }
    
    if (tableView == self.tableView_3) {
        return [self _tableView_3:tableView numberOfRowsInSection:section];
    }

    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableView_1) {
        return [self _tableView_1:tableView cellForRowAtIndexPath:indexPath];
    }
    
    if (tableView == self.tableView_2) {
        return [self _tableView_2:tableView cellForRowAtIndexPath:indexPath];
    }
    
    if (tableView == self.tableView_3) {
        return [self _tableView_3:tableView cellForRowAtIndexPath:indexPath];
    }
    
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (tableView == self.tableView_1) {
        [self _tableView_1:tableView didSelectRowAtIndexPath:indexPath];
    }
    
    if (tableView == self.tableView_2) {
        [self _tableView_2:tableView didSelectRowAtIndexPath:indexPath];
    }
    
    if (tableView == self.tableView_3) {
        [self _tableView_3:tableView didSelectRowAtIndexPath:indexPath];
    }
    
}

#pragma mark - 待审核 tableivew action -

- (NSInteger)_tableView_1:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.preAuditListModel.list.count;
}

- (UITableViewCell *)_tableView_1:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BDBusinessModel* model = self.preAuditListModel.list[indexPath.row];
    
    BDPreAuditCell *cell = [tableView dequeueReusableCellWithIdentifier:preAuditCellReuseIdentifier forIndexPath:indexPath];
    
    cell.shopNameLabel.text = model.shortName;
    cell.comitDateLabel.text = [NSString dateStringFrom1970:model.updateTime/1000 formatter:@"yyyy-MM-dd HH:mm:ss"];
    return cell;
}

- (void)_tableView_1:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HSYLog(@"do nothing");
    BDBusinessModel* model = self.preAuditListModel.list[indexPath.row];
    [self pushToShopInfoWithModel:model];
}

#pragma mark - 审核未通过 tableivew action -

- (NSInteger)_tableView_2:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.auditFailListModel.list.count;
}

- (UITableViewCell *)_tableView_2:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BDBusinessModel * model = self.auditFailListModel.list[indexPath.row];

    BDAuditFailCell *cell = [tableView dequeueReusableCellWithIdentifier:auditFailCellReuseIdentifier forIndexPath:indexPath];
    
    cell.shopNameLabel.text = model.shortName;
    cell.failureResonLabel.text = model.checkErrorInfo;

    return cell;
}

- (void)_tableView_2:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //完善资料
    BDBusinessModel* model = self.auditFailListModel.list[indexPath.row];
    [self failToFillInfoModel:model];

}

#pragma mark - 资料未提交 tableivew action -

- (NSInteger)_tableView_3:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.infoNotSubmitListModel.list.count;
}

- (UITableViewCell *)_tableView_3:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BDBusinessModel * model = self.infoNotSubmitListModel.list[indexPath.row];
    __weak typeof(self) wSelf = self;
    
    BDInfoNotSubmitCell *cell = [tableView dequeueReusableCellWithIdentifier:infoNotSubmitCellReuseIdentifier forIndexPath:indexPath];
    
    cell.shopNameLabel.text = model.shortName ? model.shortName : model.name;
    cell.continueBlock = ^{
        [wSelf continueFillInfoModel:model];
    };
    
    return cell;
}

- (void)_tableView_3:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HSYLog(@"do nothing");
    BDBusinessModel * model = self.infoNotSubmitListModel.list[indexPath.row];
    [self continueFillInfoModel:model];

}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
